package com.ruyuan.eshop.common.dao.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruyuan.eshop.common.dao.OrderItemsDAO;
import com.ruyuan.eshop.common.domain.entity.OrderItemsDO;
import com.ruyuan.eshop.common.domain.vo.OrderSkuVO;
import com.ruyuan.eshop.common.enums.CommentStatusEnum;
import com.ruyuan.eshop.common.mapper.OrderItemsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 订单货物DAO
 * @author pwd
 * @version 1.0
 */
@Repository
public class OrderItemsDAOImpl extends ServiceImpl<OrderItemsMapper, OrderItemsDO> implements OrderItemsDAO {

    @Autowired
    private OrderItemsMapper orderItemsMapper;

    @Override
    public void updateItemsCommentStatus(String orderSn, Long goodsId, CommentStatusEnum commentStatus) {
        new UpdateChainWrapper<>(orderItemsMapper)
                //设置评论状态
                .set("comment_status", commentStatus.name())
                //拼接订单编号修改条件
                .eq("order_sn", orderSn)
                //拼接商品id修改条件
                .eq("goods_id", goodsId)
                //提交修改
                .update();
    }

    @Override
    public void updateRefundPrice(String orderSn, OrderSkuVO orderSkuVO, double refundPrice) {
        new UpdateChainWrapper<>(orderItemsMapper)
                //设置可退款金额
                .set("refund_price", refundPrice)
                //拼接订单编号修改条件
                .eq("order_sn", orderSn)
                //拼接商品id修改条件
                .eq("goods_id", orderSkuVO.getGoodsId())
                //拼接货品id修改条件
                .eq("product_id", orderSkuVO.getSkuId())
                //提交修改
                .update();
    }

    @Override
    public Integer countByStatus(String orderSn, String commentStatus) {
        Integer count = new QueryChainWrapper<>(orderItemsMapper)
                //拼接订单编号查询条件
                .eq("order_sn", orderSn)
                //拼接评论状态查询条件
                .eq("comment_status", commentStatus)
                //查询数量
                .count();
        return count;
    }

    @Override
    public List<OrderItemsDO> orderItems(String orderSn) {
        List<OrderItemsDO> list = new QueryChainWrapper<>(orderItemsMapper)
                //按订单编号查询
                .eq("order_sn", orderSn)
                //列表查询
                .list();
        return list;
    }
}
